﻿#region itdc

/************************************************************************
*Copyright(C) 2015 by qsgy
 *All rights reserved. 
 *FileName:     $safeitemname$
 *Author:       oyh
 *Version:      
 *UnityVersion：
 *Date:   $time$      
 *Description:    
 *History: 
************************************************************************/
#endregion
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using DG.Tweening;
public class PassManage : MonoBehaviour {
    
    private int score;//获取分数并处理分数
    bool isNew = true;
    private int checkpoint;//记录关卡
    [SerializeField, Tooltip("标注关卡有几关")]
    private int cpCount;
    #region 设置字段，获取组件
            [SerializeField, Tooltip("返回按钮")]
            private Button ret;
            [SerializeField, Tooltip("下一关按钮")]
            private Button next;
            [SerializeField, Tooltip("显示得分")]
            private Text scoreText;
            [SerializeField, Tooltip("动画框")]
            private Image frame;
            #endregion
    /// <summary>
    /// 改变记录的关卡序号
    /// </summary>
    private void Start()
    {
        
        int.TryParse(GameManager.Instance.text.GetComponent<Text>().text,out score);
        frame = GameObject.Find("PassPanel/VictoryFrame").GetComponent<Image>();
        frame.DOFade(0,1f).SetLoops(-1,LoopType.Restart);
#region 保存记录
        checkpoint = SceneManager.GetActiveScene().buildIndex;//获取当前关卡所在的场景序号
        if (checkpoint - 2 == cpCount)
        {
            isNew = false;
        }
        TextClass.Instance.Save( TextClass.Instance.Load(false,isNew, checkpoint-2, score), "CpScore.yzc");//调用函数保存记录
        #endregion

#region 获取组件并添加事件
        ret = GameObject.Find("PassPanel/Return").GetComponent<Button>();
        ret.onClick.AddListener(PropManager.Instance.ReturnCp);
        next = GameObject.Find("PassPanel/Next").GetComponent<Button>();
        next.onClick.AddListener(NextCp);
        scoreText = GameObject.Find("PassPanel/Score").GetComponent<Text>();
        scoreText.text = score.ToString();
#endregion


    }
    public void NextCp()//进入下一关
    {
        if (isNew)
            LoadManager.Instance.Loading(checkpoint + 1);
        else LoadManager.Instance.Loading(2);
    }
}
